<?
/*
 @文件名:iMVC_Front.php
 @类  名:iMVC_Front
 @简  介:iMVC框架中前段路由
 @版  本:1.0
 @时  间:2011年10月11日
 @作  者:Dull
 */
class iMVC_Front{
    protected $ctr='index';//controller
    protected $act='index';//action
    protected $dir="./";
    protected $ruleList=array();   
    protected $errorDir='include/debug';
    protected $errorCtr='debug';
    protected $errorAct='error404';
    protected $debugSwicth=false;
    protected $debug;

    function __construct(){

        require_once($this->dir.$this->ctr.'.php');
        $this->debug=new iMVC_Debugger(); 

    }
    //设置默认


    //转发函数disPath<<<
    public function disPath(){
        $rPath=substr($_SERVER['REQUEST_URI'],strrpos($_SERVER['PHP_SELF'],'/')+1);
        $this->debug->log("request_uri:".$_SERVER['REQUEST_URI']);
        $this->debug->log("path:".$_SERVER['PHP_SELF']);
        $this->debug->log("请求的url:".$rPath);  
        //除去?号后的queryString
        if(strrpos($rPath,'?')){
            $rPath=substr($rPath,0,strrpos($rPath,'?'));
            $this->debug->log("path:".$rPath);
            //print_r($_GET);
        }
        $this->debug->log("除掉queryString后的url:".$rPath);  
        //按照权重排序
        for($i=0;$i<count($this->ruleList)-1;$i++){
            for($j=0;$j<count($this->ruleList)-$i-1;$j++){
                if($this->ruleList[$j][1]>$this->ruleList[$j+1][1]){
                    $tmp=$this->ruleList[$j+1];  
                    $this->ruleList[$j+1]=$this->ruleList[$j];
                    $this->ruleList[$j]=$tmp;
                }
            } 
        }    
        $realMatch=array();
        for($i=0;$i<count($this->ruleList);$i++){
            //遍历rules<<<
            $rRule=$this->ruleList[$i][0] ;
            $rRule=preg_replace('/\{(.*?)\}/','(?<$1>.*?)',$rRule);
            $match=array();
            $rRule=addcslashes($rRule,'/');
            $rRule='!'.$rRule.'[\/]*$!';
            if(preg_match($rRule,$rPath,$match)){

                $this->dir=$this->ruleList[$i][2]; 
                $realMatch=$match;
                if(count($this->ruleList[$i][3])>0){
                   $this->debug->log("test<br>");
                    foreach($this->ruleList[$i][3] as $rKey=>$rValue){

                        $realMatch[$rKey]=$rValue; 

                    }
                    $this->debug->log(implode(',',$realMatch));
                }
                $dispatcher=$this->ruleList[$i][4];

            };
            $this->debug->log("遍历id:".$i.",rule:".implode(',',$realMatch));
            //>>>
        }
        //相关调试注释<<<
        $this->debug->log("匹配规则后的数组:".implode(',',$realMatch));
        $this->debug->log("Controller,dir:".$this->dir);
        //print_r($realMatch);
        //>>>
        $match=$realMatch;
        $ctrFullName=$match['ctr']==null?$this->ctr:$match['ctr'];
        $ctrFullName.='Controller';
        $ctrFullName=str_replace('/','',$ctrFullName);
        $action=str_replace('/','',$action);
        $action=$action==null?$this->act:$action;
        $action=$match['act']==null?$this->act:$match['act'];
        if(file_exists($this->dir.'/'.$ctrFullName.'.php')&&!$this->debugSwicth){
            require_once($this->dir.'/'.$ctrFullName.'.php');
        }
        else{
        
            $this->debug->log("没有找到controller:");
            $_GET['ctr']=$ctrFullName;
            $_GET['act']=$action;
            $ctrFullName=$this->errorCtr;
            $ctrFullName.='Controller';
            $action=$this->errorAct;
            require_once($this->errorDir.'/'.$ctrFullName.'.php');
            $_GET['debug']=$this->debug;
        
        }
        $this->debug->log("导向成功:ctr:".$ctrFullName.",act:".$action);
             
        $dispatcher->disPatch($ctrFullName,$action,$match);
        //print_r(explode('/',$rPath));
        //print_r($_GET);
    }
    //>>>
    //添加Rule<<<
    public function addRule($rulePartten,$ruleArray=null,$piroity=1,$dir=null,$dispatcher=null){
        $dispatcher=$dispatcher==null?new iMVC_Dispatcher_Default():$dispatcher;
        $rule=array(); 
        $rule[0]=$rulePartten;
        $rule[1]=$piroity;
        $rule[2]=$dir==null?$this->dir:$dir;
        $rule[3]=$ruleArray;
        $rule[4]=$dispatcher;
        $this->ruleList[]=$rule;
    
    }
    //>>>
    //setDefalutDir设置默认controller目录
    public function setDefalutDir($dir){
        $this->dir=$dir; 
    }
    
    
}

function __autoload($classname){
    require_once($classname.'.php');
}
 

?>
